package cn.sykj.car.controller;


import cn.sykj.car.common.ResponseBean;
import cn.sykj.car.common.StatusCode;
import cn.sykj.car.entity.ConfigCardCategory;
import cn.sykj.car.entity.Users;
import cn.sykj.car.service.IConfigCardCategoryService;
import cn.sykj.car.service.IUsersService;
import cn.sykj.car.utils.Utils;
import cn.sykj.car.vo.ResponseMessageVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

/**
 * <p>
 * 会员卡类型配置 前端控制器
 * </p>
 *
 * @author duanzis
 * @since 2021-06-26
 */
@RestController
@Slf4j
@RequestMapping("/configCardCategory")
public class ConfigCardCategoryController extends BaseController{
    @Resource
    private IConfigCardCategoryService iConfigCardCategoryService;
    @Resource
    private IUsersService iUsersService;

    /**
     * 添加会员卡
     *
     * @param configCardCategory
     * @param
     * @return
     * @throws Exception
     */
    @ApiOperation(hidden = false, value = "添加会员卡", notes = "添加会员卡")
    @ApiImplicitParams({})
    @RequiresAuthentication
    @RequiresPermissions("configCardCategory:create")
    @RequestMapping(value = "/addCardCategory", method = RequestMethod.POST)
    public ResponseBean addCardCategory(@RequestBody ConfigCardCategory configCardCategory) throws Exception {

        // 获取登陆系统用户名，存入日志
        Object obj = SecurityUtils.getSubject().getPrincipal();
        log.debug((String) obj);

        Users user = iUsersService.getUserByName((String) obj);

        configCardCategory.setOperateTime(new Date());
        configCardCategory.setOperateUser(user.getUserName());


        ResponseMessageVO responseMessageVO = iConfigCardCategoryService.addCardCategory(configCardCategory);
        if (Utils.notEmpty(responseMessageVO) && responseMessageVO.isRetFlag()) {
            this.writeLog2Manager(responseMessageVO);
        }
        return new ResponseBean(StatusCode.OK, responseMessageVO);
    }

    /**
     * 删除会员信息
     *
     * @param id
     * @param
     * @return
     * @throws Exception
     */
    @ApiOperation(hidden = false, value = "删除会员卡信息", notes = "删除会员卡信息")
    @ApiImplicitParams({})
    @RequiresAuthentication
    @RequiresPermissions("configCardCategory:delete")
    @RequestMapping(value = "/{id}/delete", method = RequestMethod.POST)
    public ResponseBean delete(@PathVariable Integer id) throws Exception {

        // 获取登陆系统用户名，存入日志
        Object obj = SecurityUtils.getSubject().getPrincipal();
        log.debug((String) obj);

        ResponseMessageVO responseMessageVO = iConfigCardCategoryService.delete(id);
        if (Utils.notEmpty(responseMessageVO) && responseMessageVO.isRetFlag()) {
            this.writeLog2Manager(responseMessageVO);
        }
        return new ResponseBean(StatusCode.OK, responseMessageVO);
    }

    /**
     * 会员卡信息查询
     *
     * @param configCardCategory
     * @param currentPage
     * @param pageSize
     * @return
     * @throws Exception
     */
    @ApiOperation(hidden = false, value = "会员卡信息查询", notes = "会员卡信息查询")
    @ApiImplicitParams({})
    @RequiresAuthentication
    @RequiresPermissions("configCardCategory:view")
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public ResponseBean pageConfigCardCategory(@RequestBody ConfigCardCategory configCardCategory,
                                    @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer currentPage,
                                    @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize
    ) throws Exception {


        // 获取登陆系统用户名，存入日志
        Object obj = SecurityUtils.getSubject().getPrincipal();
        log.debug((String) obj);

        QueryWrapper<ConfigCardCategory> queryWrapper = new QueryWrapper<>();
        if(Utils.notEmpty(configCardCategory.getCardName())){
            queryWrapper.like(ConfigCardCategory.CARD_NAME,configCardCategory.getCardName());
        }
        if(Utils.notEmpty(configCardCategory.getCardType())){
            queryWrapper.eq(ConfigCardCategory.CARD_TYPE,configCardCategory.getCardType());
        }
        IPage<ConfigCardCategory> page = new Page(currentPage, pageSize);
        page =iConfigCardCategoryService.page(page,queryWrapper);
        return new ResponseBean(StatusCode.OK, page);
    }

    /**
     * 修改
     *
     * @param configCardCategory
     * @return
     * @throws Exception
     */
    @ApiOperation(hidden = false, value = "修改", notes = "修改")
    @ApiImplicitParams({})
    @RequiresAuthentication
    @RequiresPermissions("configCardCategory:edit")
    @RequestMapping(value = "/updateCardCategory", method = RequestMethod.POST)
    public ResponseBean updateCardCategory(@RequestBody ConfigCardCategory configCardCategory) throws Exception {

        // 获取登陆系统用户名，存入日志
        Object obj = SecurityUtils.getSubject().getPrincipal();
        log.debug((String) obj);

        // 当前登录人的获取
        Users user = iUsersService.getUserByName((String) obj);
        configCardCategory.setOperateTime(new Date());
        configCardCategory.setOperateUser(user.getUserName());

        ResponseMessageVO responseMessageVO = iConfigCardCategoryService.updateCardCategory(configCardCategory);
        this.writeLog2Manager(responseMessageVO);
        return new ResponseBean(StatusCode.OK, responseMessageVO);
    }
}
